package com.jkgl.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jkgl.JKMessageApplication;
import com.jkgl.common.core.domain.entity.SysUser;
import com.jkgl.common.utils.SecurityUtils;
import com.jkgl.config.WebSocketConfig;
import com.jkgl.entity.UserInfoSelfEntity;
import com.jkgl.service.UserInfoSelfService;
import com.jkgl.system.service.ISysRoleService;
import com.jkgl.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;


@SpringBootTest(classes =
        JKMessageApplication.class )
@Slf4j
@RunWith(SpringRunner.class)
public class test {
    @Resource
    private ISysUserService service;
    @Resource
    private UserInfoSelfService selfService;
    @Resource
    private ISysRoleService roleService;


    @Test
    public void connRole(){
        List<Long> collect = service.list().stream().filter(u -> u.getDelFlag().equals("0") && (
                roleService.selectRolesByUserId(u.getUserId()) != null
                        ))
                .map(SysUser::getUserId).collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 注册用户
     */
    @Test
    public void addUser(){
        for(int i = 0; i < 1000; i++) {
            log.info("添加用户{}",i);
            SysUser sysUser = new SysUser();
            sysUser.setNickName("test"+i);
            sysUser.setDelFlag("0");
            sysUser.setEmail("271227642@qq.com");
            sysUser.setPhonenumber("11111111111");
            sysUser.setPassword(SecurityUtils.encryptPassword("1234567890"));
            sysUser.setStatus("0");
            sysUser.setSex(String.valueOf(i%2));
            sysUser.setUserName("test"+i);
            service.insertUser(sysUser);
        }
    }


    @Test
    public void self(){
        int i=0;
        QueryWrapper<SysUser> userQuery = new QueryWrapper<>();
        userQuery.lambda().eq(SysUser::getDelFlag,0);
        List<Long> collect = service.list(userQuery).stream().map(SysUser::getUserId).collect(Collectors.toList());
        for(Long id : collect){
            String height = generateRandomHeight(); // 随机身高
            String weight = generateRandomWeight(); // 随机体重
            String targetStep = generateRandomTargetStep(); // 随机目标步数
            String targetWeight = generateRandomTargetWeight(weight); // 随机目标体重
            int age = ageRandom();
            UserInfoSelfEntity entity = new UserInfoSelfEntity();
            entity.setUserId(id);
            entity.setHeight(height);
            entity.setWeight(weight);
            entity.setTargetStep(targetStep);
            entity.setTargetWeight(targetWeight);
            entity.setAge(age);
            selfService.create(entity);
            log.info(String.valueOf(i++));
        }
    }
    // 随机生成身高（范围：150cm - 200cm）
    private static String generateRandomHeight() {
        Random random = new Random();
        int height = 150 + random.nextInt(51); // 150 - 200
        return String.valueOf(height);
    }

    // 随机生成体重（范围：100kg - 200kg）
    private static String generateRandomWeight() {
        Random random = new Random();
        double weight = 50 + random.nextDouble() * 25; // 40 - 120
        return String.format("%.1f", weight); // 保留一位小数
    }

    // 随机生成目标步数（范围：5000 - 20000）
    private static String generateRandomTargetStep() {
        Random random = new Random();
        int targetStep = 5000 + random.nextInt(15001); // 5000 - 20000
        return String.valueOf(targetStep);
    }

    // 随机生成目标体重（基于当前体重，范围：当前体重 -10kg 到 +10kg）
    private static String generateRandomTargetWeight(String currentWeight) {
        Random random = new Random();
        double weight = Double.parseDouble(currentWeight);
        double targetWeight = weight - 10 + random.nextDouble() * 20; // 当前体重 -10kg 到 +10kg
        return String.format("%.1f", targetWeight); // 保留一位小数
    }




    // 随机生成是否吸烟（1: 是，2: 否）
    private static int generateRandomIsSmoke() {
        Random random = new Random();
        return random.nextBoolean() ? 1 : 2; // 50% 概率
    }

    private int ageRandom(){
        int min = 13;  // 最小年龄
        int max = 60; // 最大年龄（可根据需求调整）
        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }


}
